package com.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class HashMapBinder {
	
	Logger logger = Logger.getLogger(HashMapBinder.class);
	
	HttpServletRequest req = null;
	private List<String> listColumns;
	
	public HashMapBinder(HttpServletRequest req){
		this.req = req;
		listColumns = new ArrayList<String>();
	}
	public void setListColumn(String columnName){
		listColumns.add(columnName);
	}
	public void bindInList(HashMap<String,Object> target){
		bindInList(target, "|");
	}
	public void bindInList(HashMap<String,Object> target, String splitValue){
		target.clear();
		Enumeration names = req.getParameterNames();
		String name = "";
		while(names.hasMoreElements()){
			name = (String)names.nextElement();
			if(listColumns.indexOf(name)>-1){
				List<String> valueList = new ArrayList<String>();
				String[] values = req.getParameter(name).split("["+splitValue+"]");
				for(int i=0;i<values.length;i++){
					logger.info(values[i]);
					valueList.add(values[i]);
				}target.put(name, valueList);
			}
			else target.put(name, req.getParameter(name));
		}
	}
	public void bind(Map<String,Object> target)
	{
		Enumeration<String> en 
		            = req.getParameterNames();
		while(en.hasMoreElements()){
			//System.out.println(en.nextElement());
			String key = en.nextElement();
			target.put(key, req.getParameter(key));
		}		
	} 
	public void bind2(Map<String,String> target)
	{
		Enumeration<String> en
		= req.getParameterNames();
		while(en.hasMoreElements()){
			//System.out.println(en.nextElement());
			String key = en.nextElement();
			target.put(key, req.getParameter(key));
		}		
	} 
}
